﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TRPOP.CMS.RegAssembly;
using System.Text.RegularExpressions;
using TRPOP.Common;
using System.Collections;
/*
 * 
 *   基本信息的处理
 * 
 */
namespace TRPOP.CMS.Template.GenerateCS
{
    /// <summary>
    /// 将cms标签替换为csharp语言
    /// </summary>
    public partial class ConvertTags
    {
        /// <summary>
        /// 解析函数 例如: [@date('yyyy-MM-dd',$PublishDate)]
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="oMatch"></param>
        /// <param name="strSpaces"></param>
        /// <param name="IsAddNote"></param>
        /// <returns></returns>
        public static string ConvertTag_Func(ref string strSource, Match oMatch, string strSpaces, bool IsAddNote)
        {
            string strReturn = string.Empty;

            //将标签前面的html字符串输出
            if (oMatch.Index > 0)
            {
                strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.Append    (\"{0}\");{1}", CommonMethord.ConvertSpecialChar(strSource.Substring(0, oMatch.Index)), Environment.NewLine);
            }

            //分析函数
            string strFuncName = oMatch.Groups["func"].Value.Trim().ToLower();
            ArrayList oParamlist = new ArrayList();
            foreach (string strTemp in oMatch.Groups["param"].Value.Trim().Split(','))
            {
                if (strTemp.IsEmpty())
                    continue;
                if (strTemp.Trim().StartsWith("'") && strTemp.Trim().EndsWith("'"))
                {
                    oParamlist.Add("\"" + strTemp.Trim().Replace("'", "") + "\"");
                }
                else if (strTemp.Trim().StartsWith("$"))
                {
                    Match oTempMatch = oTags_AllTypeVar.Match("[" + strTemp.Trim() + "]");
                    oParamlist.Add(string.Format("GetTagValue(\"{0}\", \"{1}\", \"{2}\")", oTempMatch.Groups["DataSet"].Value.Trim(), oTempMatch.Groups["index"].Value.Trim(), oTempMatch.Groups["var"].Value.Trim()));
                }
                else
                {
                    oParamlist.Add(strTemp.Trim());
                }
            }

            //添加解析函数的csharp代码
            strReturn += strSpaces + string.Format(strTagAnayliseBegin, oMatch.Value, Environment.NewLine);
            strReturn += strSpaces + "olistParams.Clear();" + Environment.NewLine;
            strReturn += strSpaces + "isSuccess = false;" + Environment.NewLine;
            for (int i = 0; i < oParamlist.Count; i++)
            {
                strReturn += strSpaces + "olistParams.Add(" + oParamlist[i].ToString() + ");" + Environment.NewLine;
            }

            switch (strFuncName.Trim().ToLower())
            {
                case "list_page":
                    strReturn += strSpaces + "olistParams.Add(oTemplatePageInfo.intShowPageNum.ToString());" + Environment.NewLine;
                    strReturn += strSpaces + "olistParams.Add(oTemplatePageInfo.strlistPageRegName);" + Environment.NewLine;
                    strReturn += strSpaces + "olistParams.Add(oTemplatePageInfo.intCurrentNodeID);" + Environment.NewLine;
                    strReturn += strSpaces + "olistParams.Add(oTemplatePageInfo.intCurrentContentID);" + Environment.NewLine;
                    strReturn += strSpaces + "olistParams.Add(oTemplatePageInfo.strSearchPageKeys);" + Environment.NewLine;
                    strReturn += strSpaces + "olistParams.Add((int)oTemplatePageInfo.enumTemplateType);" + Environment.NewLine;
                    strReturn += strSpaces + "olistParams.Add(oTemplatePageInfo.intListPagePubCount);" + Environment.NewLine;
                    break;
                case "searchsplit":
                    strReturn += strSpaces + "olistParams.Add(oTemplatePageInfo.intCurrentNodeID);" + Environment.NewLine;
                    break;
                default:
                    break;

            }
            strReturn += strSpaces + string.Format("oTemplatePageInfo.strBuildHtml.Append    (ExeCuteFunc({0}, olistParams));{1}",
                "\"" + strFuncName.Trim() + "\"",
                Environment.NewLine);
            strReturn += strSpaces + string.Format(strTagAnayliseEnd, oMatch.Value, Environment.NewLine);

            //去除标签
            strSource = strSource.Substring(oMatch.Index).Replace(oMatch.Value, "");
            return strReturn;
        }
    }
}